{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "106d0c1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 face++ 人脸检测/face detect"
   ]
  },
  {
   "cell_type": "raw",
   "id": "221ae894",
   "metadata": {},
   "source": [
    "## API实践步骤\n",
    "> 1.导入需要的requests模块\n",
    "> 2.输入我们需要API网站注册的API_Key\n",
    "> 3.目标url [base url]\n",
    "> 4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "> 5.准备symbol ? 后面的数据,这里需要注意,一定要详细阅读API文档中的 “参数功能”,按照要求格式准备payload参数功能可能有:\n",
    "        是否必要?必要的一定要准备好\n",
    "        选填的一定是功能,要根据功能需求 好好填噢\n",
    "> 6.response响应。注意:\n",
    "        详细阅读文档,注意请求方式(GET、POST、DELETE)\n",
    "        注意json 和字典的差异 ,str vs dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "522dacc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ZGS2rJswGzDrJnJVw1W9a1ll4WCT6a-h'\n",
    "API_sercret = 'O4QTL2e_flIJ4f0CWfN8-ey1eaYxJfrX'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "   'api_key':'ZGS2rJswGzDrJnJVw1W9a1ll4WCT6a-h',\n",
    "    'api_secret':'O4QTL2e_flIJ4f0CWfN8-ey1eaYxJfrX',\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0105bf40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61f4e925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648372716,fd3c06c3-d5fe-4e90-9285-df37b968589c',\n",
       " 'time_used': 282,\n",
       " 'faces': [{'face_token': '38659afe65bf0dbb4cbba634cd96daa1',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "16a72222",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5dcaaeac",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c0c17be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'38659afe65bf0dbb4cbba634cd96daa1'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54378dbe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "87c89ce2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bf7f5cdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ZGS2rJswGzDrJnJVw1W9a1ll4WCT6a-h'\n",
    "API_sercret = 'O4QTL2e_flIJ4f0CWfN8-ey1eaYxJfrX'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':'ZGS2rJswGzDrJnJVw1W9a1ll4WCT6a-h',\n",
    "    'api_secret':'O4QTL2e_flIJ4f0CWfN8-ey1eaYxJfrX',\n",
    "    'face_tokens': '0e75678ba5578d7c89cba2b6e356eaf4',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b9cc86fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c05774fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 46,\n",
       " 'error_message': 'INVALID_FACE_TOKEN: 0e75678ba5578d7c89cba2b6e356eaf4',\n",
       " 'request_id': '1648372720,632bf1f2-391e-4dd5-9239-35c4d699b26e'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5944cb8c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de7e04ee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c6602a52",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eee0db8",
   "metadata": {},
   "source": [
    "(这里的图片我不懂封装函数和本地打开，我直接贴地址了)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "636b7b76",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'ZGS2rJswGzDrJnJVw1W9a1ll4WCT6a-h'\n",
    "API_sercret = 'O4QTL2e_flIJ4f0CWfN8-ey1eaYxJfrX'\n",
    "def face_detect(API_key,API_sercret,image_path):\n",
    "    \"\"\"该函数为调用face++ face_detect接口\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file':'https://gitee.com/xzhichao/api_ai_ml/raw/master/week03/Liu_01.jpeg'}\n",
    "    payload = {\n",
    "            'api_key':API_key,\n",
    "            'api_secret':API_sercret\n",
    "            }\n",
    "    \n",
    "\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    return r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "256a9519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "271524d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648373273,2ed6aa63-d550-42fb-b017-a570f351555b',\n",
       " 'time_used': 327,\n",
       " 'faces': [{'face_token': 'd4e11eb0006ce4c31687825aecea365d',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试\n",
    "face_detect(API_key,API_sercret,'https://gitee.com/xzhichao/api_ai_ml/raw/master/week03/Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ca1e54c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_meta_data = face_detect(API_key,API_sercret,'https://gitee.com/xzhichao/api_ai_ml/raw/master/week03/Liu_01.jpeg')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "87f85b7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'40b263d54b04468eb2b965d23007bae1'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Liu_meta_data_face_token = Liu_meta_data['faces'][0]['face_token']\n",
    "Liu_meta_data_face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "462e3549",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 给出人脸图片进行相似度查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "eeea8b0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648373395,6e38b7ae-f4e4-4bab-8fb8-8583c8e7120d',\n",
       " 'time_used': 487,\n",
       " 'faces': [{'face_token': 'a44a95a040e05c206af5bdc3ea2b046c',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 准备测试人脸图片 face2\n",
    "\n",
    "face_detect(API_key,API_sercret,'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "fd29c2ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e16fe5208df7ce62e24acb17bcf3bc14'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face02_token = face_detect(API_key,API_sercret,'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7')['faces'][0]['face_token']\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "de5dca64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸对比\n",
    "\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':Liu_meta_data_face_token,\n",
    "    'face_token2':face02_token\n",
    "}\n",
    "r = requests.post(compare_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "20d3b172",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "025122cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648373466,616704d7-9cee-4ec2-aae4-ffbb6112c8d4',\n",
       " 'time_used': 315,\n",
       " 'confidence': 92.081,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bb30625",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caafeb1b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f988092",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c026e319",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2f13c44",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7784b001",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc12f0e8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7efc02ab",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
